package com.example.batteryhelper;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.Ohuang.ilivedata.MyLiveData;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyService extends Service {
    private BatteryManager batteryManager;
    private ExecutorService executorService;
    public  volatile boolean activityFinish = false;

    @Override
    public void onCreate() {
        super.onCreate();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notification(); //启动前台通知
        }

    }



    //循环读取电池电流值
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            while (!activityFinish) {
                int l = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW);
                MyLiveData.getInstance().with("1", Integer.class)
                        .postValue(l);

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };


    public class MyBinder extends Binder {
        public void show(String s){
            showPower(s);
        }

        @SuppressLint("WrongConstant")
        public void getPower(){
            batteryManager = (BatteryManager) getSystemService("batterymanager");
            executorService = Executors.newSingleThreadExecutor();
            executorService.execute(runnable);
        }

        public void stop(){
            activityFinish=true;
        }
    }

    MyBinder myBinder=new MyBinder();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }

    private static final String CHANNEL_ID2 = "Channel2";
    public int notifId_1 = 0x1;

    public void notification() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel();//创建通道
            NotificationCompat.Builder repliedNotification = new NotificationCompat.Builder(this, CHANNEL_ID2);
            repliedNotification.setSmallIcon(R.drawable.ic_launcher_foreground)
                    .setContentText("用于查看当前功耗").setContentTitle("电流查看助手")
                    .setOngoing(true);

            Notification notification = repliedNotification.build();
            startForeground(notifId_1, notification);
        }
    }

    public void createNotificationChannel() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID2, "退出界面显示的通知", importance);
            channel.setImportance(NotificationManager.IMPORTANCE_HIGH);//重要性
            channel.enableLights(false);
            channel.setSound(null, null);
            channel.enableVibration(false);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

    public void showPower(String i) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationCompat.Builder repliedNotification = new NotificationCompat.Builder(this, CHANNEL_ID2);
            repliedNotification.setSmallIcon(R.drawable.ic_launcher_foreground)
                    .setContentText(i).setContentTitle("电流查看助手")
                    .setOngoing(true);

            Notification notification = repliedNotification.build();
            startForeground(notifId_1, notification);
        }
    }
}